// pages/home/collection/index.js
const app = getApp();
const {
  AskPage
} = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({
  name: "collection",
  mixins: [formMixin],
  data: {
    templateName: "",
    list: [
    ],
    showShare: false,
    options: [{
      name: '微信',
      icon: 'wechat',     
    }],
  },


  bindKeyInput: function (e) {
    this.setData({
      templateName: e.detail.value
    })
  },
  search() {
    this.initData(this.data.templateName);
  },

  listTap(e) {
    let Index = e.currentTarget.dataset.parentindex, //获取点击的下标值
      list = this.data.list;
    list[Index].show = !list[Index].show || false; //变换其打开、关闭的状态
    if ((Index + 1) % 2 == 1) {
      if ((Index + 1) != list.length) {
        list[Index + 1].show = !list[Index + 1].show || false;
      }
    } else {
      list[Index - 1].show = !list[Index - 1].show || false;
    }

    this.setData({
      list
    });
  },
  //点击里面的子列表
  listItemTap(e) {
    let parentindex = e.currentTarget.dataset.parentindex, //点击的内层所在的最外层列表下标
      Index = e.currentTarget.dataset.index, //点击的内层下标
      list = this.data.list;
   // console.log(list[parentindex].item, Index, list[parentindex].item[Index],list[parentindex].item[Index].id);
    this.setData({
      list
    });
    //+"&id=1369912983278452736"
    wx.navigateTo({
      url: "/pages/home/collection/collection-details/index?taskId=" + list[parentindex].item[Index].id
    });
  
  },



  //让所有的展开项，都变为收起
  packUp(data, index) {
    for (let i = 0, len = data.length; i < len; i++) { //其他最外层列表变为关闭状态
      if (index != i) {
        data[i].show = false;
        for (let j = 0; j < data[i].item.length; j++) { //其他所有内层也为关闭状态
          data[i].item[j].show = false;
        }
      }
    }
  },
  look_over_more(e) {
    console.log("更多")
    let Index = e.currentTarget.dataset.parentindex, //获取点击的下标值
      list = this.data.list;
    wx.navigateTo({
      url: "/pages/home/collection/collection-more/index?departmentId=" + list[Index].item[0].departmentId
    });
  },
  share(e) {
    this.setData({
      showShare: true
    });
  },
  onClose() {
    this.setData({
      showShare: false
    });
  },

  onSelect(event) {
    // Toast(event.detail.name);
    this.onClose();
  },

  onShow() {
    this.initData();
    wx.login({
      success (res){
        console.log(res.code)
      }
    })
  },


 

  async initData(templateName) {
    try {

      const response = await this.$_server("form/getDeptForm", {
        data: {
          templateName: templateName
        },
      });
      let {
        data
      } = response;
      if (data.code !== 200) {
        throw data;
      }
      // console.log('---');
      console.log(data.data);
      let list = [];
      for (const key in data.data) {
        const element = data.data[key];
        list.push({
          listName: key.replace('\"', '').replace('\"', ''),
          item: element.map(cur => {
            return {
              id: cur.id || "",
              departmentId: cur.departmentId || "",
              itemName: cur.formName || ""
            }
          })
        })
      }
      this.setData({
        list: list
      })
    } catch (error) {
      console.log(error);
      if (error.message) {
        wx.showToast({
          mask: true,
          title: error.message,
          icon: "none",
          duration: 2000,
        });
      } else {
        throw error;
      }
    }
  }
});